!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash busco-5.2.2.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]] && echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]] && echo "请输入正确的安装路径" && exit 1

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
yum install -y environment-modules wget python3 python3-pip python3-numpy python3-devel valgrind flex libtool numactl tcsh
pip3 install pandas biopython

set -e
wget -t 10 -c -P $buildpath https://gitlab.com/ezlab/busco/-/archive/5.2.2/busco-5.2.2.tar.gz
tar xvf $buildpath/busco-5.2.2.tar.gz -C $buildpath && rm -rf $buildpath/busco-5.2.2.tar.gz
cd $buildpath/busco-5.2.2
python3 setup.py install --prefix=$installpath/busco-5.2.2

cat >"$installpath/busco-5.2.2/busco_modulefiles" <<EOF
#%Module1.0
conflict busco
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set BUSCO \$pwd
setenv BUSCO \$BUSCO
prepend-path PATH \$BUSCO/bin
prepend-path LD_LIBRARY_PATH \$BUSCO/lib
prepend-path PYTHONPATH \$BUSCO/lib/python3.7/site-packages
EOF
rm -rf $buildpath/busco-5.2.2
